//
//  CLAEViewController.m
//  CareLink
//
//  Created by w on 16/8/18.
//  Copyright © 2016年 Seven Team. All rights reserved.

#import "AppDelegate.h"
#import "CLAEViewController.h"
#import "CLClickBtnOtherViewController.h"
#import "CLDrawViewController.h"
#import "CLForgetPWViewController.h"
#import "CLPopWindowViewController.h"
#import "HXEasyCustomShareView.h"
#import <SVProgressHUD.h>

//主登录窗口
@interface CLAEViewController () {
    //添加的视图1
    UIImageView* addView;
    //添加的视图2
    UIImageView* View;
    //边框背景
    UIView* bgView;
    //账号文本
    UITextField* user;
    //密码文本
    UITextField* pwd;
    //额外添加的第三方登录图标
    UIButton* QQBtn;
    UIButton* weixinBtn;
    UIButton* xinlangBtn;
}
/**
 *  三个额外的登录接口属性(微信,QQ)
 */
@property (copy, nonatomic) NSString* accountNumber;
@property (copy, nonatomic) NSString* mmmm;
@property (copy, nonatomic) NSString* user;
//@property(copy,nonatomic) UIButton * landBtn;
@property (copy, nonatomic) NSString* userName;
@property (copy, nonatomic) NSString* passWord;
@end

@implementation CLAEViewController

#pragma mark navBae的隐藏
//加载视图就隐藏nav
- (void)viewWillAppear:(BOOL)animated
{
    UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
    self.navigationItem.backBarButtonItem = backItem;
    //    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

//self.view.backgroundColor=[UIColor colorWithRed:240/255.0f green:240/255.0f blue:240/255.0f alpha:1];
//    self.navigationItem.hidesBackButton = YES;
#pragma mark 快医背景图片 (快医素材)
    self.view.backgroundColor = [UIColor colorWithRed:240 / 255.0f green:240 / 255.0f blue:240 / 255.0f alpha:1];
    //self.view.frame.size.width, self.view.frame.size.height
    View = [[UIImageView alloc] initWithFrame:CGRectMake(147, 30, 100, 100)];
    //View.backgroundColor=[UIColor redColor];
    View.image = [UIImage imageNamed:@"product_img_logo_nor.png"];
    [self.view addSubview:View];

    addView = [[UIImageView alloc] initWithFrame:CGRectMake(147, 130, 100, 50)];
    //View.backgroundColor=[UIColor redColor];
    addView.image = [UIImage imageNamed:@"product_text_logo_nor.png"];
    [self.view addSubview:addView];

//第三方登录按钮

//    //用户登录label
//    UILabel *lanel = [[UILabel alloc]initWithFrame:CGRectMake((self.view.frame.size.width-50)/2, 30, 77, 30)];
//    lanel.text = @"用户登录";
//    lanel.textColor = [UIColor orangeColor];
//    //lanel.textColor = [UIColor colorWithRed:248/255.0f green:144/255.0f blue:34/255.0f alpha:1];
//    lanel.textColor = [UIColor whiteColor];
//    [self.view addSubview:lanel];

#pragma mark 在方法中可选择需要跳转的界面 clickAddBtn
    //添加的自定义的三个按钮
    UIButton* but = [[UIButton alloc] initWithFrame:CGRectMake(5, 27, 35, 35)];
    [but setImage:[UIImage imageNamed:@"goback_back_orange_on"] forState:UIControlStateNormal];
    //按钮的监听
    [but addTarget:self action:@selector(clickAddBtn:) forControlEvents:UIControlEventTouchUpInside];

//    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:but];
//    [self.view addSubview:but];

#pragma mark 通过短信注册
    UIButton* zhuce = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 255, 321, 150, 30)];
    [zhuce setTitle:@"通过短信验证码登录" forState:UIControlStateNormal];
    [zhuce setTitleColor:[UIColor colorWithRed:8 / 255.0 green:65 / 255.0 blue:130 / 255.0 alpha:0.8] forState:UIControlStateNormal];
    //[zhuce setTitleColor:[UIColor colorWithRed:248/255.0f green:144/255.0f blue:34/255.0f alpha:1] forState:UIControlStateNormal];
    zhuce.font = [UIFont systemFontOfSize:14];
    //监听注册按钮
    [zhuce addTarget:self action:@selector(zhuceAccount) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:zhuce];
    //测试
    NSLog(@"通过短信验证码点击了%@", zhuce);

    [self createButtons];
    /**
     *  分割线的视图
     */
    [self createImageViews];
    [self createTextFields];
#pragma mark 最后添加的功能
    //    [self createLabel];
}
//点击按钮
- (void)clickAddBtn:(UIButton*)button
{
    self.view.backgroundColor = [UIColor whiteColor];
    //exit函数中的实参是返回给操作系统,exit(0)正常退出 exit(1)非正常退出
    exit(0);
}
//账号密码的文本布局
- (void)createTextFields
{
    CGRect frame = [UIScreen mainScreen].bounds;
    bgView = [[UIView alloc] initWithFrame:CGRectMake(10, 200, frame.size.width - 20, 100)];
    //设置图层圆角
    bgView.layer.cornerRadius = 3.0;
    bgView.alpha = 0.7;
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    //主界面账号
    user = [self createTextFielfFrame:CGRectMake(60, 10, 271, 30) font:[UIFont systemFontOfSize:14] placeholder:@"请输入您手机号码"];
    //user.text=@"118701646234";
    //密码样式带删除功能
    user.keyboardType = UIKeyboardTypeNumberPad;
    user.clearButtonMode = UITextFieldViewModeWhileEditing;
    //主界面密码
    pwd = [self createTextFielfFrame:CGRectMake(60, 60, 271, 30) font:[UIFont systemFontOfSize:14] placeholder:@"请输入密码"];
    pwd.clearButtonMode = UITextFieldViewModeWhileEditing;
    //pwd.text=@"123456";
    //密码样式带删除功能
    pwd.secureTextEntry = YES;
    pwd.keyboardType = UIKeyboardTypeNumberPad;

    //设置登录账号密码背景图标
    UIImageView* userImageView = [self createImageViewFrame:CGRectMake(23, 15, 16, 20) imageName:@"注册-手机号" color:nil];
    UIImageView* pwdImageView = [self createImageViewFrame:CGRectMake(20, 60, 25, 25) imageName:@"mm_normal" color:nil];
    //中间的分割线
    UIImageView* line1 = [self createImageViewFrame:CGRectMake(20, 50, bgView.frame.size.width - 40, 1) imageName:nil color:[UIColor colorWithRed:180 / 255.0f green:180 / 255.0f blue:180 / 255.0f alpha:.3]];

    //添加根视图
    [bgView addSubview:user];
    [bgView addSubview:pwd];
    [bgView addSubview:userImageView];
    [bgView addSubview:pwdImageView];
    [bgView addSubview:line1];
}
//结束响应
- (void)touchesEnded:(nonnull NSSet<UITouch*>*)touches withEvent:(nullable UIEvent*)event
{
    [user resignFirstResponder];
    [pwd resignFirstResponder];
}

//开始响应
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    [user resignFirstResponder];
    [pwd resignFirstResponder];
}
#pragma mark 分割线
- (void)createImageViews
{
    //    UIImageView *userImageView=[self createImageViewFrame:CGRectMake(25, 10, 25, 25) imageName:@"ic_landing_nickname" color:nil];
    //    UIImageView *pwdImageView=[self createImageViewFrame:CGRectMake(25, 60, 25, 25) imageName:@"ic_landing_password" color:nil];
    //    UIImageView *line1=[self createImageViewFrame:CGRectMake(25, 50, 260, 1.5) imageName:nil color:[UIColor lightGrayColor]];
    //
    //    //UIImageView *line2=[self createImageViewFrame:CGRectMake(88, 210, 280, 1) imageName:nil color:[UIColor grayColor]];

    //    UIImageView* line3 = [self createImageViewFrame:CGRectMake(2, 450, 122, 1) imageName:nil color:[UIColor lightGrayColor]];
    //    UIImageView* line4 = [self createImageViewFrame:CGRectMake(self.view.frame.size.width - 120, 450, 135, 1) imageName:nil color:[UIColor lightGrayColor]];

    //    [bgView addSubview:userImageView];
    //    [bgView addSubview:pwdImageView];
    //    [bgView addSubview:line1];
    //[self.view addSubview:line2];
    //    [self.view addSubview:line3];
    //    [self.view addSubview:line4];
}
#pragma mark 三个功能按钮
- (void)createButtons
{
    //    [self.navigationController pushViewController:[[CLAEViewController alloc]init] animated:YES];
    UIButton* landBtn = [self createButtonFrame:CGRectMake(10, 350, self.view.frame.size.width - 20, 37) backImageName:nil title:@"登录" titleColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:19] target:self action:@selector(landClickBtn)];
    //    [_landBtn setImage:[UIImage imageNamed:@"nav"] forState:UIControlStateNormal];
    landBtn.backgroundColor = [UIColor colorWithRed:8 / 255.0 green:65 / 255.0 blue:130 / 255.0 alpha:0.8];
    //landBtn.backgroundColor=[UIColor clearColor];
    landBtn.layer.cornerRadius = 5.0f;

    UIButton* newUserBtn = [self createButtonFrame:CGRectMake(57, 384, 62, 30) backImageName:nil title:@"加入快医" titleColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:14] target:self action:@selector(registration:)];
    //    newUserBtn.backgroundColor=[UIColor orangeColor];
    //lightGray
    [newUserBtn setTitle:@"加入快医" forState:UIControlStateNormal];
    [newUserBtn setTitleColor:[UIColor colorWithRed:8 / 255.0 green:65 / 255.0 blue:130 / 255.0 alpha:0.8] forState:UIControlStateNormal];
    UIButton* forgotPwdBtn = [self createButtonFrame:CGRectMake(self.view.frame.size.width - 107, 384, 62, 30) backImageName:nil title:@"忘记密码" titleColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:14] target:self action:@selector(fogetPwd:)];
    //forgotPwdBtn.backgroundColor = [UIColor orangeColor];
    [forgotPwdBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
    [forgotPwdBtn setTitleColor:[UIColor colorWithRed:8 / 255.0 green:65 / 255.0 blue:130 / 255.0 alpha:0.8] forState:UIControlStateNormal];

//    //第三方登录按钮
//    UIButton* ThreeBtn = [self createButtonFrame:CGRectMake(self.view.frame.size.width - 260, 437, 150, 30) backImageName:nil title:@"忘记密码" titleColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:13] target:self action:@selector(threeBtn:)];
//    //forgotPwdBtn.backgroundColor = [UIColor orangeColor];
//    [ThreeBtn setTitle:@"۩    使用第三方登录    ۩" forState:UIControlStateNormal];
//    //8/255.0 green:65/255.0 blue:130/255.0 alpha:0.8] forState:UIControlStateNormal
//    //灰色
//    [ThreeBtn setTitleColor:[UIColor colorWithRed:8 / 255.0 green:65 / 255.0 blue:130 / 255.0 alpha:0.8] forState:UIControlStateNormal];

#define Start_X 60.0f // 第一个按钮的X坐标
#define Start_Y 440.0f // 第一个按钮的Y坐标
#define Width_Space 50.0f // 2个按钮之间的横间距
#define Height_Space 20.0f // 竖间距
#define Button_Height 50.0f // 高
#define Button_Width 50.0f // 宽

    //    //微信
    //    weixinBtn = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 25) / 2, 459,  33, 33)];
    //    //weixinBtn.tag = UMSocialSnsTypeWechatSession;
    //    weixinBtn.layer.cornerRadius = 25;
    //    weixinBtn = [self createButtonFrame:weixinBtn.frame backImageName:@"微信1" title:nil titleColor:nil font:nil target:self action:@selector(clickWX:)];
    //    //QQ
    //    QQBtn = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width - (-20)) / 2 - 65, 459, 33, 33)];
    //    //QQBtn.tag = UMSocialSnsTypeMobileQQ;
    //    QQBtn.layer.cornerRadius = 25;
    //    QQBtn = [self createButtonFrame:QQBtn.frame backImageName:@"qq" title:nil titleColor:nil font:nil target:self action:@selector(clickQQ:)];
    //
    //    //新浪微博
    //    xinlangBtn = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 50) / 2 + 55, 459,  33, 33)];
    //    //xinlangBtn.tag = UMSocialSnsTypeSina;
    //    xinlangBtn.layer.cornerRadius = 25;
    //    xinlangBtn = [self createButtonFrame:xinlangBtn.frame backImageName:@"weibo" title:nil titleColor:nil font:nil target:self action:@selector(clickSina:)];

    [self.view addSubview:weixinBtn];
    [self.view addSubview:QQBtn];
    [self.view addSubview:xinlangBtn];
    [self.view addSubview:landBtn];
    [self.view addSubview:newUserBtn];
    [self.view addSubview:forgotPwdBtn];
}

- (UITextField*)createTextFielfFrame:(CGRect)frame font:(UIFont*)font placeholder:(NSString*)placeholder
{
    UITextField* textField = [[UITextField alloc] initWithFrame:frame];

    textField.font = font;
    //grayColor 灰色
    textField.textColor = [UIColor grayColor];

    textField.borderStyle = UITextBorderStyleNone;

    textField.placeholder = placeholder;

    return textField;
}

- (UIImageView*)createImageViewFrame:(CGRect)frame imageName:(NSString*)imageName color:(UIColor*)color
{
    UIImageView* imageView = [[UIImageView alloc] initWithFrame:frame];

    if (imageName) {
        imageView.image = [UIImage imageNamed:imageName];
    }
    if (color) {
        imageView.backgroundColor = color;
    }

    return imageView;
}

- (UIButton*)createButtonFrame:(CGRect)frame backImageName:(NSString*)imageName title:(NSString*)title titleColor:(UIColor*)color font:(UIFont*)font target:(id)target action:(SEL)action
{
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    if (imageName) {
        [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    }

    if (font) {
        btn.titleLabel.font = font;
    }

    if (title) {
        [btn setTitle:title forState:UIControlStateNormal];
    }
    if (color) {
        [btn setTitleColor:color forState:UIControlStateNormal];
    }
    if (target && action) {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}
/**
 *  跳转界面到通过点击进行验证码登录
 */
- (void)zhuceAccount
{
    //登录跳转
    CLClickBtnOtherViewController  *vc = [[CLClickBtnOtherViewController  alloc]init];
    vc.number = 1;
    [self.navigationController pushViewController:vc animated:YES];
    //测试短信登录界面调用
    NSLog(@"开启短信登录界面");
}
/**
 *  加入快医跳转
 */
- (void)registration:(UIButton*)button
{
    CLClickBtnOtherViewController  *vc = [[CLClickBtnOtherViewController  alloc]init];
    vc.number = 2;
    // 记录用户名
    
    
    [self.navigationController pushViewController:vc animated:YES];

}
/**
 *
 *
 *  忘记密码跳转
 */
- (void)fogetPwd:(UIButton*)button
{
    [self.navigationController pushViewController:[[CLForgetPWViewController alloc]init] animated:YES];
}
//第三方分享
- (void)threeBtn:(UIButton*)button
{
    [self addGuanjiaShareView];
}
#pragma mark - 手机号格式化工具
//让输入的密码使用*代替
- (NSString*)getHiddenStringWithPhoneNumber:(NSString*)number middle:(NSInteger)countHiiden
{
    // if (number.length>6) {

    if (number.length < countHiiden) {
        return number;
    }
    NSInteger count = countHiiden;
    NSInteger leftCount = number.length / 2 - count / 2;
    NSString* xings = @"";
    for (int i = 0; i < count; i++) {
        xings = [NSString stringWithFormat:@"%@%@", xings, @"*"];
    }

    NSString* chuLi = [number stringByReplacingCharactersInRange:NSMakeRange(leftCount, count) withString:xings];
    return chuLi;
}
#pragma mark - 手机号格式化后还原
- (NSString*)getHiddenStringWithPhoneNumber1:(NSString*)number middle:(NSInteger)countHiiden
{
    // if (number.length>6) {
    if (number.length < countHiiden) {
        return number;
    }
    NSString* xings = @"";
    for (int i = 0; i < 1; i++) {

    }

    NSString* chuLi = [number stringByReplacingCharactersInRange:NSMakeRange(0, 0) withString:@""];

    return chuLi;
}

- (void)clickQQ:(UIButton*)button
{

}

- (void)clickWX:(UIButton*)button
{
}

- (void)clickSina:(UIButton*)button
{
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark 第三方分享内容
- (void)addGuanjiaShareView
{
    NSArray* shareAry = @[ @{ @"image" : @"shareView_wx",
                              @"title" : @"微信" },
        @{ @"image" : @"shareView_friend",
            @"title" : @"朋友圈" },
        @{ @"image" : @"shareView_qq",
            @"title" : @"QQ" },
        @{ @"image" : @"shareView_wb",
            @"title" : @"新浪微博" },
        @{ @"image" : @"shareView_rr",
            @"title" : @"人人网" },
        @{ @"image" : @"shareView_qzone",
            @"title" : @"QQ空间" },
        @{ @"image" : @"shareView_msg",
            @"title" : @"短信" },
        @{ @"image" : @"share_copyLink",
            @"title" : @"复制链接" } ];

    UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGMMainScreenWidth, 54)];
    headerView.backgroundColor = [UIColor clearColor];

    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, headerView.frame.size.width, 15)];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor blackColor];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:15];
    label.text = @"分享第三方登录";
    [headerView addSubview:label];

    UILabel* lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, headerView.frame.size.height - 0.5, headerView.frame.size.width, 0.5)];
    lineLabel.backgroundColor = [UIColor colorWithRed:208 / 255.0 green:208 / 255.0 blue:208 / 255.0 alpha:1.0];
    [headerView addSubview:lineLabel];

    UILabel* lineLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, headerView.frame.size.width, 0.5)];
    lineLabel1.backgroundColor = [UIColor colorWithRed:208 / 255.0 green:208 / 255.0 blue:208 / 255.0 alpha:1.0];

    HXEasyCustomShareView* shareView = [[HXEasyCustomShareView alloc] initWithFrame:CGRectMake(0, 0, CGMMainScreenWidth, CGMMainScreenHeight)];
    shareView.backView.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1.0];
    shareView.headerView = headerView;
    float height = [shareView getBoderViewHeight:shareAry firstCount:7];
    shareView.boderView.frame = CGRectMake(0, 0, shareView.frame.size.width, height);
    shareView.middleLineLabel.hidden = YES;
    [shareView.cancleButton addSubview:lineLabel1];
    shareView.cancleButton.frame = CGRectMake(shareView.cancleButton.frame.origin.x, shareView.cancleButton.frame.origin.y, shareView.cancleButton.frame.size.width, 54);
    shareView.cancleButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [shareView.cancleButton setTitleColor:[UIColor colorWithRed:184 / 255.0 green:184 / 255.0 blue:184 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    [shareView setShareAry:shareAry delegate:self];
    //[self.navigationController.view addSubview:shareView];
}

#pragma mark 用户名和密码判断
- (void)landClickBtn
{
    //获取已经存储的账户名和密码
    NSString* userName = [[NSUserDefaults standardUserDefaults] objectForKey:KUserName];
    NSString* passWord = [[NSUserDefaults standardUserDefaults] objectForKey:KpassWord];
    self.userName = userName;
    self.passWord = passWord;

    if ([user.text isEqualToString:userName] && [pwd.text isEqualToString:passWord]) {
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:KisLoad];
        [self.navigationController popViewControllerAnimated:YES];
    }
    else {
        [self landClick];
    }
}
#pragma mark 第三方框架 SVProgressHUD  用来网络加载和提示错误
//登录
- (void)landClick
{
    if ([user.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"亲,请输入用户名"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
    }
    else if (user.text.length < 11) {
        [SVProgressHUD showErrorWithStatus:@"您输入的手机号码格式不正确"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
    }
    else if ([pwd.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"亲,请输入密码"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });

    }
    else if (![user.text isEqualToString:self.userName]) {
        UIAlertController* ac = [UIAlertController alertControllerWithTitle:@"  提示  " message:@" 该账户名不存在，请重新输入账户名和密码，若没有账户，请先注册！ " preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* confirm = [UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleCancel handler:^(UIAlertAction* _Nonnull action){

        }];
        [ac addAction:confirm];
        [self presentViewController:ac animated:true completion:^{
        }];
    }
    else if (![pwd.text isEqualToString:self.passWord]) {
        [SVProgressHUD showErrorWithStatus:@"亲,密码与账号不匹配"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });

    }
}

@end
